using System.Text;
using TrackSystem.IO.Data;
using TrackSystem.IO.Models;


namespace TrackSystem.IO.Protocol
{
    /// <summary>
    /// 高度计通信协议
    /// 0--Depth
    /// 1--Temperature
    /// 2--Heading
    /// 3--Pitch
    /// 4--Roll
    /// </summary>
    public class ISA500Protocol : ProtocolBase
    {
        private int startTicks;
        private bool bSendR;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public ISA500Protocol(string name)
            : base()
        {
            NeedInit = false;
            Name = name;
            bSendR = false;
        }

        /// <summary>
        /// 生成读取寄存器的通讯帧
        /// </summary>
        /// <returns></returns>
        public override byte[] GenerateReadFrame(out int NeedRecvLen)
        {
            startTicks = Environment.TickCount;
            if (bSendR)
            {
                Fail_R++;
                // 失败超过3次，成功次数清零
                if (Fail_R >= 3)
                {
                    Succ_R = 0;
                }
            }
            bSendR = true;
            NeedRecvLen = 8;
            return null;
        }


        /// <summary>
        /// 根据收到的内容进行处理
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public override bool ProcessReadFrame(byte[] buffer)
        {
            Rx_r = buffer;
            Lapse = Environment.TickCount - startTicks;
            try
            {
                string frame = Encoding.UTF8.GetString(buffer);
                string[] ar_line = frame.Split('\n');
                bool bISADS = true;
                bool bISHPR = true;
                for (int i = 0; i < ar_line.Length; i++)
                {
                    string[] ar_str = ar_line[i].Split(',');
                    if (bISADS && ar_str.Length == 5 && ar_str[0].Trim() == "$ISADS")
                    {
                        RegFactory.Instance.SetVal(RegNameList[0], double.Parse(ar_str[1]));
                        RegFactory.Instance.SetVal(RegNameList[1], double.Parse(ar_str[3]));
                        bISADS = false;
                    }

                    if (bISHPR && ar_str.Length == 4 && ar_str[0].Trim() == "$ISHPR")
                    {
                        RegFactory.Instance.SetVal(RegNameList[2], double.Parse(ar_str[1]));
                        RegFactory.Instance.SetVal(RegNameList[3], double.Parse(ar_str[2]));
                        string[] str_2 = ar_str[3].Split('*');
                        RegFactory.Instance.SetVal(RegNameList[4], double.Parse(str_2[0]));
                        bISHPR = false;
                    }
                    if (!bISHPR && !bISADS)
                    {
                        break;
                    }
                }
                if (!bISHPR || !bISADS)
                {
                    Succ_R++;
                    bSendR = false;     // 正确收到，标志位变为false
                    Fail_R = 0;         // 通信成功，则将失败次数清零
                }
            }
            catch (Exception)
            {
                return false;
            }
            //Succ_R++;
            //bSendR = false;     // 正确收到，标志位变为false
            //Fail_R = 0;         // 通信成功，则将失败次数清零
            return true;
        }


        /// <summary>
        /// 生成写入寄存器的通讯帧
        /// </summary>
        /// <param name="NeedRecvLen"></param>
        /// <returns></returns>
        public override byte[] GenerateWriteFrame(out int NeedRecvLen)
        {
            startTicks = Environment.TickCount;

            NeedRecvLen = 0;
            return null;
        }

        /// <summary>
        /// 根据收到的内容进行处理
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public override bool ProcessWriteFrame(byte[] buffer)
        {
            Lapse = Environment.TickCount - startTicks;
            return true;
        }
    }
}
